import {
	MODULE_DICTID
} from '@/const/modules/index.js'
import {
	parseTime,
	checkType
} from '@/utils'
import {
	debounce
} from '@/static/common/js/util.js'
import {
	updateArchivesTime,
	getLocalArchivesDetail,
	getLocalNotice,
	createLocalNotice
} from '@/static/common/js/padLocalFileHandler.js'
export default {
	data() {
		return {
			userInfo: {},
			currentRow: {},
			archivesData: {},
			formData: {},
			keyId: null,
			params: {},
			loading: false,
			pdfDownload: false, // 控制底部按钮
			isEdit: false, // 控制页面当前是否预览状态
		};
	},
	computed: {
		headers() {
			return {
				Authorization: 'Bearer ' + this.vuex_token
			};
		},
		currentModule() {
			const currentDictId = this.params.dictId;
			const currentModule = Object.entries(MODULE_DICTID).find(([key, value]) => value == currentDictId);
			if (currentModule) {
				const [moduleName, moduleId] = currentModule;
				return {
					moduleName,
					moduleId
				}
			}
			return {}
		},
		defaultPersonName() {
			return this.userInfo && this.userInfo.nickName || this.currentRow.archives && this.currentRow.archives
				.doPerson
		},
		// false 代表离线， true 代表在线
		isNetworkConnected() {
			return this.vuex_config.isConnected
		},
	},
	async onLoad(params) {
		this.params = params
		const {
			dictId,
			keyId,
			noticeId,
			pdfDownload,
			isEdit,
			userId,
			archivesId
		} = params
		this.pdfDownload = pdfDownload == 'true'
		this.isEdit = isEdit == 'true'
		if (this.beforeHandleData) {
			this.beforeHandleData(params)
		} else {
			if (!dictId) return console.log('请选择对应模块')
		}
		// #ifdef APP-PLUS || APP
		let result = await getLocalArchivesDetail(archivesId)
		this.currentRow = result
		this.archivesData = result
		// #endif
		if (userId) {
			this.userInfo = (await this.$u.api.systemUser.get(userId))?.data || {}
		}
		this.keyId = parseInt(keyId)
		if (keyId === '0') {
			this.formData = Object.assign({}, this.initForm())
		} else {
			// #ifdef APP-PLUS || APP
			let data = await getLocalNotice(archivesId, dictId, keyId)
			this.formData = Object.assign({}, this.initForm(), data)
			// #endif
		}
		console.log(this.formData);
		if (this.afterHandleData) {
			this.afterHandleData(this.formData)
		}

	},
	methods: {
		async submitFormBackBefore(done) {
			const {
				moduleName,
				moduleId
			} = this.currentModule
			const {
				dictId,
				keyId,
				noticeId,
				pdfDownload,
				isEdit,
				userId,
				archivesId
			} = this.params
			// return
			// uni.showLoading({
			// 	title: '加载中',
			// 	mask: true
			// });
			if (this.submitFormBefore) {
				await this.submitFormBefore()
			}
			if (this.formData.createTime) {
				delete this.formData.createTime
			}
			let times = []
			for (let key in this.formData) {
				if (key.includes('Time')) {
					times.push(key)
				}
			}
			times.forEach(item => {
				this.formData[item] = parseTime(this.formData[item])
			})

			if (this.formData.fireAddress && checkType(this.formData.fireAddress) == 'array') {
				this.formData.fireAddress = this.formData.fireAddress.join(',')
			}
			console.log(archivesId);
			console.log(this.formData);
			// #ifdef APP-PLUS || APP
			await createLocalNotice(archivesId, dictId, keyId, keyId == '0', this.formData)
			await updateArchivesTime(archivesId)
			// #endif
			uni.showToast({
				title: '操作成功',
				mask: true
			});
			done()
		},
		debouncedCreateLocalNotice: debounce(async (archivesId, dictId, keyId, isAdd, form) => {
			await createLocalNotice(archivesId, dictId, keyId, isAdd, form)
		}, 1000),
		async submitForm() {
			try {
				await this.submitFormBackBefore(() => {
					uni.hideLoading();
					this.backPrevPage()
				})
			} catch (error) {
				console.error(error);
				uni.hideLoading();
			}
		},
		handleCancel() {
			this.formData = Object.assign({}, this.initForm())
			this.backPrevPage()
		},
		backPrevPage() {
			var pages = getCurrentPages(); // 当前页
			var prevPage = pages[pages.length - 2]; //上个页面
			uni.navigateBack({
				delta: 1,
				success() {
					// 使用 $vm 监听调用 上一页面的 Refresh() 方法
					prevPage.$vm.Refresh();
				}
			})
		}
	},
};